%{
#include <math.h>
#include "common.h"
int yylex(void);
void yyerror(char const *);

%}
%define api.value.type union
%nterm <struct TagNode *> var var_
%token <char*> ID "tag"
%token <char*> TYPE "type"
%token EOL
%token _N END EUQL
%%
input: line

line:
|TYPE var  END line   { 
                        value1 *head = createvalue1();
                        printf("input\n");
                        if(!$2){
                            printf("error tag");
                        }
                        else{
                            head->tags = $2;
                        }
                        printNode(head->tags);
}
;

var:ID var_          {  printf("var\n");
                        if(!$2){
                            printf("arg1 is var %s\n", $1);
                           $2 = CreateTagNode($1); 
                        }
                        else{
                            InsertTag($2, $1);
                        }
                        $$=$2;
};

var_:',' ID var_    {
                    printf("input var_\n");
                    if(!$3){
                        $3 = CreateTagNode($2);

                    }
                    else{
                        InsertTag($3, $2);
                    }
                    $$=$3;
                    
}
|          {printf("empty var_\n");$$=NULL;}
;                   

%%

#include <ctype.h>

void yyerror (char const *s) {
    extern int yylineno;    
    extern char *yytext;    
    //int len=strlen(yytext);
    //int i;
    //char buf[512]={0};
    //for (i=0;i<len;++i)
    //{
    //    sprintf(buf,"%s%c",buf,yytext[i]);
    //}
    //fprintf(stderr, "ERROR: %s at symbol '%s' on line %d, str is %s \n", s, buf, yylineno, yytext);
    fprintf(stderr, "ERROR \"%s\" \n", yytext);
}
 

int main (void) {
    lexmain();
    yyparse();
}